//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.block
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.10 黑名单
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoBlock extends DoAPI
	{
		public function DoBlock(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_CREATE_BLOCK:String = 'http://api.t.163.com/blocks/create.json';
		public static const CMD_CREATE_BLOCK:String = 'cmdCreateBlock';
		
		private static const URL_DESTROY_BLOCK:String = 'http://api.t.163.com/blocks/destroy.json';
		public static const CMD_DESTROY_BLOCK:String = 'cmdDestroyBlock';
		
		private static const URL_EXIST_BLOCK:String = 'http://api.t.163.com/blocks/exists.json';
		public static const CMD_EXIST_BLOCK:String = 'cmdExistBlock';
		
		private static const URL_GET_BLOCK:String = 'http://api.t.163.com/blocks/blocking.json';
		public static const CMD_GET_BLOCK:String = 'cmdGetBlock';
		
		private static const URL_GET_BLOCK_IDS:String = 'http://api.t.163.com/blocks/blocking/ids.json';
		public static const CMD_GET_BLOCK_IDS:String = 'cmdGetBlockIds';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 阻止指定用户
		 * 
		 * @param user_id  可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 */		
		public function createBlock(user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_CREATE_BLOCK, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_CREATE_BLOCK, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_CREATE_BLOCK, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_CREATE_BLOCK, params);
			}
		}
		
		/**
		 * 取消已阻止的用户
		 * 
		 * @param user_id  可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 */		
		public function destroyBlock(user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_DESTROY_BLOCK, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_DESTROY_BLOCK, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DESTROY_BLOCK, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DESTROY_BLOCK, params);
			}
		}
		
		/**
		 * 判断指定用户是否已被阻止
		 *  
		 * @param user_id 可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 */		
		public function existBlock(user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(URL_EXIST_BLOCK, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_EXIST_BLOCK, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_EXIST_BLOCK, params);
			}
		}
		
		/**
		 * 查看已阻止的用户列表
		 */		
		public function getBlock():void
		{
			var paras:Array = [];
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_BLOCK, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_BLOCK, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_BLOCK, params);
			}
		}
		
		/**
		 * 查看已阻止的用户列表id数组
		 */		
		public function getBlockIds():void
		{
			var paras:Array = [];
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_BLOCK_IDS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_BLOCK_IDS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_BLOCK_IDS, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
	}
}